package com.niu1078.joe.net;

import android.content.Context;
import android.provider.Telephony;

import com.niu1078.joe.net.callback.IError;
import com.niu1078.joe.net.callback.IFailure;
import com.niu1078.joe.net.callback.IRequest;
import com.niu1078.joe.net.callback.ISuccess;
import com.niu1078.joe.net.callback.RequestCallbacks;
import com.niu1078.joe.net.download.DownloadHandler;
import com.niu1078.joe.ui.loader.JoeLoader;
import com.niu1078.joe.ui.loader.LoaderStyle;

import java.io.File;
import java.util.Map;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Body;

/**
 * author :ywq .
 * time: 2017/12/18:19:31.
 * desc :
 * action:
 */

public class RestClient {

    private final String URL;
    private final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();
    private final IRequest REQUEST;
    private final String DOWNLOAD_DIR;
    private final String EXTENSION;
    private final String NAME;

    private final IError ERROR;
    private final IFailure FAILURE;
    private final ISuccess SUCCESS;
    private final RequestBody BODY;
    private final LoaderStyle LOADER_STYLE;
    private final Context CONTEXT;
    private final File FILE;


    public RestClient(String url,
                      WeakHashMap<String, Object> params,
                      String downloadDir,
                      String extension,
                      String name,
                      IRequest request,
                      IError error,
                      IFailure failure,
                      ISuccess success,
                      File file,
                      RequestBody body
            , LoaderStyle LoaderStyle
            , Context context
    ) {

        this.URL = url;
//        this.PARAMS = params;
        PARAMS.putAll(params);
        this.REQUEST = request;
        this.DOWNLOAD_DIR=downloadDir;
        this.EXTENSION=extension;
        this.NAME=name;
        this.ERROR = error;
        this.FAILURE = failure;
        this.SUCCESS = success;
        this.BODY = body;
        this.FILE = file;
        this.LOADER_STYLE = LoaderStyle;
        this.CONTEXT = context;
    }

    public static RestClientBuilder builder() {

        return new RestClientBuilder();
    }

    public void request(HttpMethod method) {
        final RestService restService = RestCreator.getRestService();

        Call<String> call = null;

        if (REQUEST != null) {
            REQUEST.onRequestStart();
        }
        if (LOADER_STYLE != null) {
            JoeLoader.showLoading(CONTEXT, LOADER_STYLE);
        }
        switch (method) {
            case GET:
                call = restService.get(URL, PARAMS);
                break;
            case POST:
                call = restService.post(URL, PARAMS);

                break;


            case PUT:
                call = restService.put(URL, PARAMS);

                break;

            case DELETE:
                call = restService.delete(URL, PARAMS);

                break;


            case POST_RAW:
                call = restService.postRaw(URL, BODY);

                break;

            case PUT_RAW:
                call = restService.putRaw(URL, BODY);

                break;

            case UPLOAD:
                final RequestBody requestBody = RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()), FILE);
                final MultipartBody.Part body = MultipartBody.Part.createFormData("file", FILE.getName(),requestBody);
                call = restService.upload(URL, body);

                break;

            default:
                break;
        }


        if (call != null) {

            call.enqueue(getRequestCalback());

        }

    }

    private Callback<String> getRequestCalback() {
        return new RequestCallbacks(REQUEST, SUCCESS, FAILURE, ERROR, LOADER_STYLE);
    }


    public final void get() {
        request(HttpMethod.GET);
    }

    public final void post() {
        if (BODY == null) {
            //如果请求体是空的 表示 PARAMS里有值 直接使用post请求
            request(HttpMethod.POST);
        } else {
            //如果不是空
            if (!PARAMS.isEmpty()) {
                //参数里面有值 就错了  BODY和PARAMS 不能都有
                throw new RuntimeException("参数必须是空的");
            }
            request(HttpMethod.POST_RAW);
        }
    }

    public final void put() {


        if (BODY == null) {
            //如果请求体是空的 表示 PARAMS里有值 直接使用post请求
            request(HttpMethod.PUT);
        } else {
            //如果不是空
            if (!PARAMS.isEmpty()) {
                //参数里面有值 就错了  BODY和PARAMS 不能都有
                throw new RuntimeException("参数必须是空的");
            }
            request(HttpMethod.PUT_RAW);
        }


    }

    public final void delete() {
        request(HttpMethod.DELETE);
    }
    public final void upLoad() { request(HttpMethod.UPLOAD);  }
    public final void downLoad() {
        new DownloadHandler(URL, REQUEST, DOWNLOAD_DIR, EXTENSION, NAME,
                SUCCESS, FAILURE, ERROR)
                .handleDownload();
    }
}
